using UnityEngine;
using System.Collections;

public class PokeBehave : MonoBehaviour {
	
	public float Velocidade, RVelocidade, Show1, Show2;
	string estado = "Idle";
	Vector3 ToGo;
	public Transform Alvo;
	AudioClip AC;
	AudioSource AS;
	public bool ativo = false;
	
	// Use this for initialization
	void Start () {
		ToGo = new Vector3(this.transform.position.x-100, this.transform.position.y, this.transform.position.z+100);
	}
	
	// Update is called once per frame
	void Update () {
		
		if(ativo)
		{
			ToGo = Alvo.transform.position;
			ToGo.y = this.transform.position.y;
			if(Dist(ToGo) > 20){
				estado = "Run";
			}else if(Dist(ToGo) > 15){
				estado = "Walk";
			}else if(Dist(ToGo) < 10){
				estado = "Idle";	
			}
			if(estado == "Run" || estado == "Walk"){
				IrPara(ToGo);
			}
			if(Input.GetKey(KeyCode.Z)){
				Show1 = 1;
				AS = new AudioSource();
				AS.clip = Microphone.Start("Built-in Microphone", false, 5, 44100);
				AS.Play();
			}
		}
		
		if(!ativo)
		{
			estado = "Idle";
		}
		this.animation.Play(estado);
	}
	
	void IrPara(Vector3 Alvo){
		//this.transform.LookAt(Alvo);
		//this.transform.Rotate(0, 90, 0);
		this.transform.rotation = new Quaternion(this.transform.rotation.x, Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(Alvo - this.transform.position), 1*Time.deltaTime).y, this.transform.rotation.z, Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(Alvo - this.transform.position), 1*Time.deltaTime).w);
		this.transform.Rotate(0, Mathf.PI/2, 0);
		if(estado == "Walk"){
			this.rigidbody.AddForce(new Vector3(0, -20, 0));
			this.rigidbody.AddRelativeForce(new Vector3(-Velocidade, 0, 0));
		}else if(estado == "Run"){
			this.rigidbody.AddForce(new Vector3(0, -50, 0));
			this.rigidbody.AddRelativeForce(new Vector3(-RVelocidade, 0, 0));
		}
	}
	
	float Dist(Vector3 Alvo){
		Vector3 ttp = this.transform.position;
		return(Mathf.Sqrt(((ttp.x-Alvo.x)*(ttp.x-Alvo.x))+((ttp.z-Alvo.z)*(ttp.z-Alvo.z))));
	}
}
